<?php
class Jnd_Form_Decorator_Formulairejndpro extends Zend_Form_Decorator_Abstract
{

    public function buildLabel()
    {
        $element = $this->getElement();
        if (! ($element instanceof Zend_Form_Element_Submit || $element instanceof Zend_Form_Element_Hash)) {
            if (! $element->noLabel) {
                $label = $element->getLabel();
                if ($translator = $element->getTranslator()) {
                    $label = $translator->translate($label);
                }
                $label .= " ";
                return $element->getView()->formLabel($element->getName(), $label, array(
                    "class" => "styleDdb"
                ));
            }
        }
        return '';
    }

    public function buildInput()
    {
        $element = $this->getElement();
        $helper = $element->helper;
        if ($element->getBelongsTo() !== null) {
            $nom = $element->getBelongsTo() . '[' . $element->getName() . "]";
        } else {
            $nom = $element->getName();
        }
        if ($element instanceof Zend_Form_Element_Radio) {
            $encadrementAvant = "<span class='decallageRadio'>";
            $encadrementApres = "</span><br />";
        } else {
            $encadrementAvant = "";
            $encadrementApres = "";
        }
        return $encadrementAvant . $element->getView()->$helper($nom, $element->getValue(), $element->getAttribs(), $element->options) . $encadrementApres;
    }

    public function buildErrors()
    {
        $element = $this->getElement();
        $messages = $element->getMessages();
        if (empty($messages)) {
            return '';
        }
        $jquery = Jnd_App::getConfig('jquery');
        $jqueryServeurUrl = $jquery['path']['min'];
        $element->setAttrib("class", $element->getAttrib("class") . " erreur");
        $element->getView()->jQuery()->addOnLoad("
                $('#tooltipError" . $element->getName() . "').tooltip({
                    track: true,
                    delay: 0,
                    showURL: false,
                    fade: 250,
                    cssClass: 'tooltip-jnd-error'
                });
            ")->addJavascriptFile('/js/tooltip/jquery.tooltip.js')->addStylesheet('/js/tooltip/jquery.tooltip.css');
        $err = $element->getView()->formErrors($messages);
        return '<br /><span class="errors"><img src="/images/erreur.png" id="tooltipError' . $element->getName() . '" alt="" border="" class="tooltip" title="' . $element->getView()->escape($err) . '"> ' . __('Il y a des erreurs') . '</span>';
        // return '<div class="errors">' .
        // $element->getView()->formErrors($messages) . '</div>';
    }

    public function buildDescription()
    {
        $element = $this->getElement();
        $desc = $element->getDescription();
        if (empty($desc)) {
            return '';
        }
        return '<br /><span style="font-size: 80%">' . $desc . '</span>';
    }

    public function buildRequired()
    {
        $element = $this->getElement();
        if (! ($element instanceof Zend_Form_Element_Submit || $element instanceof Zend_Form_Element_Hash)) {
            if ($element->isRequired()) {
                return " requis";
            }
        }
        return '';
    }

    public function render( $content )
    {
        $element = $this->getElement();
        if (! $element instanceof Zend_Form_Element) {
            return $content;
        }
        if (null === $element->getView()) {
            return $content;
        }
        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        if (! $element instanceof Zend_Form_Element_Hidden) {
            $label = $this->buildLabel();
            $input = $this->buildInput();
            $errors = $this->buildErrors();
            $desc = $this->buildDescription();
            $required = $this->buildRequired();
            $output = "<p class='relook $required'>" . $label . $input . $errors . $desc . '</p>';
            switch ($placement)
            {
                case (self::PREPEND):
                    return $output . $separator . $content;
                case (self::APPEND):
                default:
                    return $content . $separator . $output;
            }
        } else {
            return $content . $separator . $this->buildInput();
        }
    }
}
